package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.Cri;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    BrandDao brandDao1;
    @Resource
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
        }
    }

    @Override
    public void updateByIds(BrandEntity brandEntity) {

//        Map<String,Object> map = new HashMap<>();
//        map.put("id",brandEntity.getBrandId());
//        map.put("name",brandEntity.getName());
//        map.put("logo",brandEntity.getLogo());
//        map.put("descript",brandEntity.getDescript());
//        map.put("show_status",brandEntity.getShowStatus());
//        map.put("first_letter",brandEntity.getFirstLetter());
//        map.put("sort",brandEntity.getSort());
//        Date date = new Date();
//        map.put("updateTime",date.toString());

        Cri cri = new Cri();
        cri.put("id",brandEntity.getBrandId());
        cri.put("name",brandEntity.getName());
    //    cri.put("id",brandEntity.getBrandId());
        brandDao1.updateById1(cri);
    }

    @Override
    public BrandEntity lookList(Long id) {

        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        BrandEntity brandEntity = brandDao1.selectList1(map);
        return brandEntity;
    }

    @Override
    public void saveList(BrandEntity brandEntity) {

         brandDao1.saveList(brandEntity);

    }

    @Override
    public List<BrandEntity> selectAll() {

       List<BrandEntity> list =  brandDao1.selectAll();
       return list;
    }


}